<?php

namespace app\admin\model\vactivities;

use think\Model;


class Activity extends Model
{



    const ACTIVITY_STATUS_0 = 0;//下架
    const ACTIVITY_STATUS_1 = 1;//上架
    const ACTIVITY_STATUS_2 = 2;//回收站


    const ACTIVITY_TAG_1 = 1;//即将开始
    const ACTIVITY_TAG_2 = 2;//已结束
    const ACTIVITY_TAG_3 = 3;//活动回收站



    //参与人选项 0 注册加入活动 1 入场劵加入活动 2 直接加入活动
    const TICKET_STATUS_0 = 0;
    const TICKET_STATUS_1 = 1;
    const TICKET_STATUS_2 = 2;


        // 表名
    protected $name = 'activity';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'isrecording_text',
        'status_text',
        'isend_text',
        'isticket_text',
    ];


    //新加状态字段
    public function getStatusList()
    {
        return ['0' => __('activity_status 0'),'1' => __('activity_status 1'),'2' => __('activity_status 2')];
    }
    //获取状态字段
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getIsrecordingList()
    {
        return ['0' => __('Isrecording 0'), '1' => __('Isrecording 1')];
    }

    public function getIsendList()
    {
        return ['0' => __('isend 0'),'1' => __('isend 1')];
    }


    public function getIsticketList()
    {
        return ['0' => __('ticket_status 0'), '1' => __('ticket_status 1'), '2' => __('ticket_status 2')];
    }

    //获取是否购买入场劵字段
    public function getIsticketTextAttr($value, $data)
    {
        $value = $value ? $value : $data['isticket'];
        $list = $this->getIsticketList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    //获取活动结束标志
    public function getIsendTextAttr($value, $data)
    {
        $value = $value ? $value : $data['isend'];
        $list = $this->getIsendList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getIsrecordingTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['isrecording']) ? $data['isrecording'] : '');
        $list = $this->getIsrecordingList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /*
     * 头部活动标志
     * $type 1 活动举办人 有回收站  2 超管数据没有回收站
     *
     */
    public function getHeadingList($type)
    {

        switch ($type){
            case  1:
                $ret = [
                    1 => [
                        'tag' => self::ACTIVITY_TAG_1,
                        'head' => __('即将开始')
                    ],
                    2 => [
                        'tag' => self::ACTIVITY_TAG_2,
                        'head' => __('已结束')
                    ],
                    3 => [
                        'tag' => self::ACTIVITY_TAG_3,
                        'head' => __('活动回收站')
                    ],

                ];
                break;

            case 2:
                $ret = [
                    1 => [
                        'tag' => self::ACTIVITY_TAG_1,
                        'head' => __('即将开始')
                    ],
                    2 => [
                        'tag' => self::ACTIVITY_TAG_2,
                        'head' => __('已结束')
                    ]

                ];
                break;
        }



        return $ret;
    }

    /*
     * 通过id查找活动名称
     */
    public function getActNameById($id){
        $r =  $this->getModel()->where('id',$id)->field('name')->find();
        if($r)
            return $r->name;
        else
            return '';
    }

    /*
     * 获取活动信息
     * $actid 活动id
     */
    public function getActivityInfo($actid){
        $r =  $this->getModel()->where('id',$actid)->field('*')->find();
        if($r)
            return $r;
        else
            return '';
    }

    /*
     * 统计发布活动数量
     */

    public function getActCntByUid($uid){
        $cnt =  $this->getModel()->where('admin_id',$uid)->where('status',self::ACTIVITY_STATUS_1)->count();

        if($cnt > 0)
            return $cnt;
        else
            return 0;
    }


}
